package com.shuqi.model;

import java.io.File;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.shuqi.common.MyTask;
import com.shuqi.common.Urls;
import com.shuqi.common.utils.Log4an;
import com.shuqi.common.utils.SharedPreferenceUtil;
import com.shuqi.interfaces.EventTypeConstants;
import com.shuqi.model.bean.SoftConfigInfo;
import com.shuqi.model.net.HttpRequest;
import com.shuqi.model.net.UrlManager;
import com.shuqi.model.net.listener.HttpRequestListener;
import com.shuqi.model.parse.parser.GetSoftConfigInfoParser;

/**
 * 获取软件配置信息
 * 
 * @author yanghuawei
 */
public class GetSoftConfigOnlineModel implements HttpRequestListener {
    private final String logTag = "GetSoftConfigOnlineModel";
    private HttpRequest httpRequest;
    private OnGetSoftConfigInfoListener listener;
    private Context context;

    public GetSoftConfigOnlineModel(Context context, OnGetSoftConfigInfoListener listener) {
        this.listener = listener;
        this.context = context;
    }

    public void loadData() {
        httpRequest =
                new HttpRequest(context, UrlManager.COLUMN_DOMAIN_APPINFO, getRequestUrl(), setParams(), this);
        httpRequest.setDataParseAdapter(new GetSoftConfigInfoParser());
        MyTask.runInBackground(httpRequest, true);
    }

    @Override
    public void action(int actionCode, Object object) {
        switch (actionCode) {
            case EventTypeConstants.EVENT_SUCCESS:// 读取数据 写处理逻辑
                SoftConfigInfo configInfo = (SoftConfigInfo) object;
                if (configInfo != null) {
                    listener.OnFinish(1, configInfo);
                    Log4an.i(logTag, configInfo.toString());
                } else {
                    listener.OnFinish(0, null);
                }
                break;
            default:
                listener.OnFinish(-1, null);
                break;
        }
    }

    @Override
    public List<BasicNameValuePair> setParams() {
        return null;
    }

    @Override
    public String getRequestUrl() {
        return addParamsForAppInfoUrl(Urls.getGetSoftConfigInfoUrl());
    }

    /***
     * 添加软件的第一次安装时间和最后更新时间
     * 
     * @param url
     * @return
     */
    private String addParamsForAppInfoUrl(String url) {

        try {
            long firstInstallTime;
            long lastUpdateTime;
            PackageManager pm = context.getPackageManager();
            try {
                PackageInfo pInfo =
                        pm.getPackageInfo(context.getPackageName(), PackageManager.GET_META_DATA);
                Class<PackageInfo> c = PackageInfo.class;
                firstInstallTime = c.getDeclaredField("firstInstallTime").getLong(pInfo);
                lastUpdateTime = c.getDeclaredField("lastUpdateTime").getLong(pInfo);
            } catch (Exception ignored) {
                ApplicationInfo info =
                        pm.getApplicationInfo(context.getPackageName(),
                                PackageManager.GET_META_DATA);
                SharedPreferences sp = context.getSharedPreferences(SharedPreferenceUtil.SP_NAME_CONFIG, Context.MODE_PRIVATE);
                firstInstallTime = sp.getLong("firstInstallTime", 0);
                lastUpdateTime = new File(info.sourceDir).lastModified();
                if (firstInstallTime == 0) {
                    firstInstallTime = lastUpdateTime;
                    Editor ed = sp.edit();
                    ed.putLong("firstInstallTime", firstInstallTime);
                    ed.commit();
                }
            }
            url += ("&aft=" + firstInstallTime + "&aut=" + lastUpdateTime);
            Log4an.e(logTag, "firstInstallTime=" + firstInstallTime + ",lastUpdateTime="
                    + lastUpdateTime);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return url;
    }

    public interface OnGetSoftConfigInfoListener {
        /**
         * 在线获取用户信息
         * 
         * @param type -1，网络失败，0，失败，1成功
         * @param info
         */
        public void OnFinish(int type, SoftConfigInfo info);
    }
}
